package org.particlethink.model;


import org.particlethink.entry.OrderStatus;
import org.particlethink.entry.RoleTyp;
import org.particlethink.handler.OrderStatusHandler;
import org.particlethink.handler.RoleTypeHandler;
import tk.mybatis.mapper.annotation.ColumnType;
import tk.mybatis.mapper.annotation.KeySql;

import javax.persistence.Id;
import javax.persistence.Table;
import java.util.Date;

@Table(name = "orders")
public class Order {//订单
    @Id
    @KeySql(useGeneratedKeys = true)
    private Integer id;
    private Date createdAt;
    private String no;//编号
    private Integer batchPlanId;//批次计划
    private String batchPlanNo;//批次计划编号
    private Integer masterPlanId;//总计划id
    private String masterPlanNo;//总计划id
    private Integer supplierId;//供货商id
    private String categoryId;//物料大类
    private Date deadlineAt;//最晚进场时间
    private Boolean hasReCreated;//是否重新发起过


    @ColumnType(typeHandler = OrderStatusHandler.class)
    private OrderStatus status;
    private String remark;//供应商拒绝原因
    private Date refuseDate;
    private Integer refuseUserId;
    @ColumnType(typeHandler = RoleTypeHandler.class)
    private RoleTyp refuseUserType;
    private Date doneDate;
    private Integer creatorId;//创建者
    private Integer projectId; //项目id
    private Boolean stopped; //是否终止
    private Boolean completed; //完成发货
    private Boolean allShipment;


    public Order() {
    }

    public Order(
            Integer batchPlanId,
            String batchPlanNo,
            Integer masterPlanId,
            String masterPlanNo,
            Integer supplierId,
            String categoryId,
            Date deadlineAt,
            Integer creatorId,
            Integer projectId
    ) {
        this.batchPlanId = batchPlanId;
        this.batchPlanNo = batchPlanNo;
        this.masterPlanId = masterPlanId;
        this.masterPlanNo = masterPlanNo;
        this.supplierId = supplierId;
        this.status = OrderStatus.Unshipped;
        this.creatorId = creatorId;
        this.createdAt = new Date();
        this.no = new Date().getTime() + "";
        this.categoryId = categoryId;
        this.deadlineAt = deadlineAt;
        this.hasReCreated = false;
        this.remark = "";
        this.projectId = projectId;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getId() {
        return id;
    }

    public void setCreatedAt(Date createdAt) {
        this.createdAt = createdAt;
    }

    public Date getCreatedAt() {
        return createdAt;
    }


    public void setBatchPlanId(Integer batchPlanId) {
        this.batchPlanId = batchPlanId;
    }

    public Integer getBatchPlanId() {
        return batchPlanId;
    }


    public void setStatus(OrderStatus status) {
        this.status = status;
    }

    public OrderStatus getStatus() {
        return status;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public String getRemark() {
        return remark;
    }

    public void setCreatorId(Integer creatorId) {
        this.creatorId = creatorId;
    }

    public Integer getCreatorId() {
        return creatorId;
    }


    public Integer getSupplierId() {
        return supplierId;
    }

    public void setSupplierId(Integer supplierId) {
        this.supplierId = supplierId;
    }

    public Integer getProjectId() {
        return projectId;
    }

    public void setProjectId(Integer projectId) {
        this.projectId = projectId;
    }

    public boolean canDeliver() {
        return (this.status == OrderStatus.Unshipped || this.status == OrderStatus.InProgress) && (!this.getStopped());
    }


    public boolean canCancel() {
        return this.status == OrderStatus.Unshipped || this.status == OrderStatus.InProgress;
    }

    public boolean canRefuse() {
        return this.status == OrderStatus.Unshipped;
    }

    public boolean canReCreate() {
        return this.status == OrderStatus.Refuse;
    }

    public boolean canRemark() {
        return this.status == OrderStatus.Unshipped;
    }

    public boolean canStop() {
        return this.status == OrderStatus.InProgress;
    }


    public boolean stopDelivery() {
        return this.status == OrderStatus.InProgress;
    }

    public String getCategoryId() {
        return categoryId;
    }

    public void setCategoryId(String categoryId) {
        this.categoryId = categoryId;
    }

    public Date getDeadlineAt() {
        return deadlineAt;
    }

    public void setDeadlineAt(Date deadlineAt) {
        this.deadlineAt = deadlineAt;
    }

    public String getNo() {
        return no;
    }

    public void setNo(String no) {
        this.no = no;
    }

    public String getBatchPlanNo() {
        return batchPlanNo;
    }

    public void setBatchPlanNo(String batchPlanNo) {
        this.batchPlanNo = batchPlanNo;
    }


    public void setHasReCreated(Boolean hasReCreated) {
        this.hasReCreated = hasReCreated;
    }

    public Integer getMasterPlanId() {
        return masterPlanId;
    }

    public void setMasterPlanId(Integer masterPlanId) {
        this.masterPlanId = masterPlanId;
    }

    public String getMasterPlanNo() {
        return masterPlanNo;
    }

    public void setMasterPlanNo(String masterPlanNo) {
        this.masterPlanNo = masterPlanNo;
    }

    public Date getRefuseDate() {
        return refuseDate;
    }

    public void setRefuseDate(Date refuseDate) {
        this.refuseDate = refuseDate;
    }

    public Date getDoneDate() {
        return doneDate;
    }

    public void setDoneDate(Date doneDate) {
        this.doneDate = doneDate;
    }

    public Integer getRefuseUserId() {
        return refuseUserId;
    }

    public void setRefuseUserId(Integer refuseUserId) {
        this.refuseUserId = refuseUserId;
    }

    public RoleTyp getRefuseUserType() {
        return refuseUserType;
    }

    public void setRefuseUserType(RoleTyp refuseUserType) {
        this.refuseUserType = refuseUserType;
    }

    public boolean isUnshipped() {
        return this.status == OrderStatus.Unshipped;
    }

    public boolean isInProgress() {
        return this.status == OrderStatus.InProgress;
    }

    public boolean isRefused() {
        return this.status == OrderStatus.Refuse;
    }

    public boolean isDone() {
        return this.status == OrderStatus.Done;
    }


    public Boolean getHasReCreated() {
        if (hasReCreated==null){
            return false;
        }
        return hasReCreated;
    }

    public Boolean getStopped() {
        if (stopped == null) {
            return false;
        }
        return stopped;
    }

    public void setStopped(Boolean stopped) {
        this.stopped = stopped;
    }

    public Boolean getCompleted() {
        if (completed==null){
            return false;
        }
        return completed;
    }

    public void setCompleted(Boolean completed) {
        this.completed = completed;
    }

    public Boolean getAllShipment() {
        if (allShipment==null){
            return false;
        }
        return allShipment;
    }

    public void setAllShipment(Boolean allShipment) {
        this.allShipment = allShipment;
    }
}
